// pages/me/me.js
import {
  $init,
  $digest,
  json2Form
} from '../../utils/common.util'

const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    height: app.globalData.height,
    width: app.globalData.width,
    barHeight: app.globalData.barHeight,
    serverUrl: app.globalData.serverUrl,
    input: '',
    tag: 1,
    images: [],
    //发布分类
    issueType: 0,
    issueVal: [],
    // tempFilePaths: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    $init(this)
    this.data.issueType = app.globalData.issueType
    this.setData({
      issueType: app.globalData.issueType
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    //获得dialog组件
    this.dialog = this.selectComponent("#dialog");
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },

  //返回
  back: function(e) {
    wx.navigateBack({
      delta: 1,
    })
  },

  //
  chooseImage(e) {
    wx.chooseImage({
      count: 6,
      sizeType: ['compressed'], //可选择原图或压缩后的图片
      sourceType: ['album', 'camera'], //可选择性开放访问相册、相机
      success: res => {
        const images = this.data.images.concat(res.tempFilePaths)
        // 限制最多只能留下6张照片
        this.data.images = images.length <= 6 ? images : images.slice(0, 6)
        $digest(this)
      }
    })
  },

  //delete
  removeImage(e) {
    const idx = e.target.dataset.idx
    this.data.images.splice(idx, 1)
    $digest(this)
  },

  //
  handleImagePreview(e) {
    const idx = e.target.dataset.idx
    const images = this.data.images
    wx.previewImage({
      current: images[idx], //当前预览的图片
      urls: images, //所有要预览的图片
    })
  },

  // 弹框事件
  showDialog() {
    //判断内容是否为空
    let issueVal = this.data.issueVal;
    let issueType = this.data.issueType;
    //判断闲置是否为空
    if (issueType == 4) {
      if (issueVal.price == '' || issueVal.content == '' || issueVal.title == '' || issueVal.phone == '' || issueVal.weChat == '') {
        wx.showToast({
          title: "请完善信息！",
          icon: 'none',
          duration: 2000
        });
      } else {
        this.dialog.showDialog();
      }
    }
    //判断快递是否为空
    if (issueType == 3) {
      if (issueVal.kdprice == '' || issueVal.kdcontent == '' || issueVal.title == '' || issueVal.phone == '' || issueVal.weChat == '') {
        wx.showToast({
          title: "请完善信息！",
          icon: 'none',
          duration: 2000
        });
      } else {
        this.dialog.showDialog();
      }
    }
    //判断家教是否为空
    if (issueType == 2) {
      if (issueVal.jjprice == '' || issueVal.jjcontent == '' || issueVal.title == '' || issueVal.phone == '' || issueVal.weChat == '') {
        wx.showToast({
          title: "请完善信息！",
          icon: 'none',
          duration: 2000
        });
      } else {
        this.dialog.showDialog();
      }
    }
    //判断帮帮是否为空
    if (issueType == 1) {
      if (issueVal.bbcontent == '' || issueVal.title == '' || issueVal.phone == '' || issueVal.weChat == '') {
        wx.showToast({
          title: "请完善信息！",
          icon: 'none',
          duration: 2000
        });
      } else {
        this.dialog.showDialog();
      }
    }
  },

  //取消事件
  _cancelEvent() {
    this.dialog.hideDialog();
  },

  //获取输入值
  formSubmit(e) {
    this.setData({
      issueVal: e.detail.value,
    })
  },

  //确认事件
  _confirmEvent() {
    this.dialog.hideDialog();
    wx.showLoading({
      title: '正在上传',
      mask: true,
    })
    //发布请求
    var that = this;
    var saleUrl = '';
    const token = wx.getStorageSync('token');
    const images = this.data.images; //图片路径

    if (token) {
      //发布闲置
      if (that.data.issueType == 4) {
        const saleUrl = app.globalData.serverUrl + "/admin/sale/addsale";
        wx.request({
          url: saleUrl,
          method: 'POST',
          data: {
            token: token,
            title: this.data.issueVal.title,
            content: this.data.issueVal.content,
            name: this.data.issueVal.name,
            phone: this.data.issueVal.phone,
            weChat: this.data.issueVal.weChat,
            price: this.data.issueVal.price,
          },
          success(res) {
            if (res.data.code == 1) {
              var len = images.length;
              if (len != 0) {
                var count = 0;
                for (var idx in images) {
                  wx.uploadFile({
                    url: saleUrl.slice(0, -4) + 'Img',
                    filePath: images[idx], // 要上传文件资源的路径 String类型！！！
                    name: 'file',
                    formData: {
                      token: token,
                      uid: res.data.uid,
                    }, // HTTP 请求中其他额外的参数
                    success(res) {
                      count++;
                    },
                    complete(res) {
                      if (count == len) {
                        wx.hideLoading();
                        wx.showToast({
                          title: '发布成功！',
                          icon: 'success',
                          duration: 2000
                        });
                        wx.redirectTo({
                          url: '/pages/sale/sale',
                        });
                      }
                    }

                  })
                }
              } else {
                wx.showToast({
                  title: '发布成功！',
                  icon: 'success',
                  duration: 2000
                });
                wx.redirectTo({
                  url: '/pages/sale/sale',
                });
              }

            } else {
              wx.showToast({
                title: '发布失败！请检查信息',
                icon: 'none',
                duration: 2000
              });
            }
          }
        })
      }

      //发布快递
      if (that.data.issueType == 3) {
        saleUrl = app.globalData.serverUrl + "/admin/express/addexpress"
        wx.request({
          url: saleUrl,
          method: 'POST',
          data: {
            token: token,
            title: this.data.issueVal.kdtitle,
            content: this.data.issueVal.kdcontent,
            time: this.data.issueVal.kdtime,
            num: this.data.issueVal.kdnum,
            name: this.data.issueVal.name,
            phone: this.data.issueVal.phone,
            weChat: this.data.issueVal.weChat,
            price: this.data.issueVal.kdprice,
          },
          success(res) {
            if (res.data.code == 1) {
              wx.showToast({
                title: '发布成功！',
                icon: 'success',
                duration: 2000
              });
              wx.redirectTo({
                url: '/pages/express/express',
              });
            } else {
              wx.showToast({
                title: '发布失败！请检查信息',
                icon: 'none',
                duration: 2000
              });
            }
          }
        })
      }

      //发布家教
      if (that.data.issueType == 2) {
        saleUrl = app.globalData.serverUrl + "/admin/teacher/addteacher"
        wx.request({
          url: saleUrl,
          method: 'POST',
          data: {
            token: token,
            price: this.data.issueVal.jjprice,
            title: this.data.issueVal.jjtitle,
            address: this.data.issueVal.jjaddress,
            time: this.data.issueVal.jjtime,
            content: this.data.issueVal.jjcontent,
            name: this.data.issueVal.name,
            phone: this.data.issueVal.phone,
            weChat: this.data.issueVal.weChat,
          },
          success(res) {
            if (res.data.code == 1) {
              var len = images.length;
              if (len != 0) {
                var count = 0;
                for (var idx in images) {
                  wx.uploadFile({
                    url: saleUrl.slice(0, -7) + 'Img',
                    filePath: images[idx], // 要上传文件资源的路径 String类型！！！
                    name: 'file',
                    formData: {
                      token: token,
                      uid: res.data.uid,
                    }, // HTTP 请求中其他额外的参数
                    success(res) {
                      count++;
                    },
                    complete(res) {
                      if (count == len) {
                        wx.hideLoading();
                        wx.showToast({
                          title: '发布成功！',
                          icon: 'success',
                          duration: 2000
                        });
                        wx.redirectTo({
                          url: '/pages/teacher/teacher',
                        });
                      }
                    }

                  })
                }
              } else {
                wx.showToast({
                  title: '发布成功！',
                  icon: 'success',
                  duration: 2000
                });
                wx.redirectTo({
                  url: '/pages/teacher/teacher',
                });
              }

            } else {
              wx.showToast({
                title: '发布失败！请检查信息',
                icon: 'none',
                duration: 2000
              });
            }
          }
        })
      }
      //  //发布帮帮
      if (that.data.issueType == 1) {
        saleUrl = app.globalData.serverUrl + "/admin/help/addhelp"
        wx.request({
          url: saleUrl,
          method: 'POST',
          data: {
            token: token,
            price: this.data.issueVal.bbprice,
            title: this.data.issueVal.bbtype,
            content: this.data.issueVal.bbcontent,
            name: this.data.issueVal.name,
            phone: this.data.issueVal.phone,
            weChat: this.data.issueVal.weChat,
            //  price: this.data.issueVal.bbprice,
          },
          success(res) {
            if (res.data.code == 1) {
              var len = images.length;
              if (len != 0) {
                var count = 0;
                for (var idx in images) {
                  wx.uploadFile({
                    url: saleUrl.slice(0, -4) + 'Img',
                    filePath: images[idx], // 要上传文件资源的路径 String类型！！！
                    name: 'file',
                    formData: {
                      token: token,
                      uid: res.data.uid,
                    }, // HTTP 请求中其他额外的参数
                    success(res) {
                      count++;
                    },
                    complete(res) {
                      if (count == len) {
                        wx.hideLoading();
                        wx.showToast({
                          title: '发布成功！',
                          icon: 'success',
                          duration: 2000
                        });
                        wx.redirectTo({
                          url: '/pages/help/help',
                        });
                      }
                    }

                  })
                }
              } else {
                wx.showToast({
                  title: '发布成功！',
                  icon: 'success',
                  duration: 2000
                });
                wx.redirectTo({
                  url: '/pages/help/help',
                });
              }

            } else {
              wx.showToast({
                title: '发布失败！请检查信息',
                icon: 'none',
                duration: 2000
              });
            }
          }
        })
      }
    } else {
      wx.showToast({
        title: '获取错误，请重新获取！',
        icon: 'success',
        duration: 2000
      });
    }
  },
})